package org.fioma.tree.bytes;

import java.io.BufferedWriter;
import java.io.IOException;

import org.fioma.tree.Node;
import org.fioma.tree.SimpleLeaf;

public class SimpleLeafBytes<I extends SimpleInternalNodeBytes<I, F>, F extends SimpleLeafBytes<I, F>> extends SimpleLeaf<byte[], I, F> {

	public SimpleLeafBytes(I parent, byte[] label) {
		this.parent = parent;
		this.label = label;
	}

	// add a new node in the position of the current label with label word.
	// The letter in the position of the current label will belong to the current label Node
	@Override
	public F addNode(byte pos, byte[] word) {
		return Tools.addNodeBytes(this, pos, word);
	}

	@Override
	public short getLengthLabel() {
		return label[0];
	}

	@Override
	public F addWord(byte[] word) {
		label = Tools.joinWords(label, word);
		return (F) this;
	}

	@Override
	public void printWords(BufferedWriter bw, byte[] prefix) throws IOException {
		byte[] b = Tools.joinWords(prefix, label);
		bw.write(Tools.transformToChars(b));
		bw.newLine();
	}

	@Override
	public boolean find(byte[] word) {
		return word == null || word[0] == 0;
	}

//	@Override
//	public int addSuffix(byte[] suffix) {
//		label = Tools.joinWords(label, suffix);
//		return 1;
//	}
//
//	@Override
//	public int addToSuffixTree(SuffixNode tree, byte[] prefix, int phase) {
//		byte[] word = Tools.joinWords(prefix, label);
//		tree.addSuffix(word, phase, 0); // start in the extension 0
//		return 0;
//	}

	@Override
	public boolean isLeaf() {
		return true;
	}

	@Override
	public Node<byte[], I, F> findExistentNode(byte[] word) {
		if (word == null || word.length == 0 || word[0] == 0) {
			return this;
		}
		return null;
	}

}
